<?php

namespace app\index\dao;

use app\common\model\SourceCollect;
use ShidianUtil\Thinkphp\Dao\DaoTrait;

/**
 * SourceCollectDao 数据访问对象
 * Class SourceCollectDao
 * @package app\index\dao
 * @method SourceCollect findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method SourceCollect insertActive(array $params, array $allow_field = [])
 * @method SourceCollect updateActive(SourceCollect $model, array $params, array $allow_field = [])
 * @method SourceCollect findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method SourceCollect updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(SourceCollect $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class SourceCollectDao
{
    use DaoTrait;

    /**
     * @var SourceCollect
     */
    private $baseModel;

    /**
     * SourceCollectDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = SourceCollect::class;
    }

    /**
     * 某用户是否收藏了某线索
     * @param $memberId
     * @param $sourceId
     * @return int
     */
    public function valueOfCollected($memberId, $sourceId){
        return SourceCollect::where('memberId', $memberId)
            ->where('sourceId', $sourceId)
            ->find() ? 1 : 0;
    }

    /**
     * 查询某用户收藏的线索ID数组
     * @param $memberId
     * @return array
     */
    public function selectIdsByMemberId($memberId): array
    {
        return SourceCollect::where('memberId', $memberId)
            ->order('created', 'DESC')
            ->select()
            ->column('sourceId');
    }
}
